<template>
 
  <view class="content" v-if="idfd==1">
	<view class="lang-picker-abs">
    <picker mode="selector" :range="langList" :value="langIndex" @change="onLangChange" class="lang-picker">
      <view class="lang-btn">{{ langList[langIndex] }}</view>
    </picker>
  </view>

    <view class="header">
      <image src="../../static/shilu-login/logo.png"></image>
    </view>
    <view class="list">
      <view class="list-call">
        <image class="img" src="/static/shilu-login/1.png"></image>
        <input class="sl-input" v-model="mobile" type="number" maxlength="11" :placeholder="$t('login.mobile')" />
      </view>
      <view class="list-call">
        <image class="img" src="/static/shilu-login/2.png"></image>
        <input class="sl-input" v-model="password" type="text" maxlength="32" :placeholder="$t('login.password')" password="true" />
      </view>
    </view>
    <view class="button-login" hover-class="button-hover" @tap="bindLogin()">
      <text style="font-size: 40upx;">{{ $t('login.login') }}</text>
    </view>
    <!-- #ifndef MP-WEIXIN -->
    <view class="agreenment">
      <navigator url="reg" open-type="navigate">{{ $t('login.register') }}</navigator>
    </view>
    <!-- #endif -->
    <!-- #ifdef MP-WEIXIN -->
    <button class="button-login" @click="getUserInfo1()" style="margin-top: 35rpx; font-size: 26upx; color: #fff; height: 80rpx; line-height: 80rpx;">{{ $t('login.wxauth') }}</button>
    <!-- #endif -->
  </view>
</template>

<script>
  export default {
    data() {
      return {
        mobile: '',
        password: '',
		openid:'',
		unionid:'',
		userinfowx:'',
		idfd:1,
		fxpid:0,
        langList: ['简体中文', 'English'],
        langValueList: ['zh', 'en'],
        langIndex: 0
      };
    },
    onLoad() {
      // 页面加载时读取缓存语言
      const cachedLang = uni.getStorageSync('app_lang');
      if (cachedLang) {
        const idx = this.langValueList.indexOf(cachedLang);
        if (idx !== -1) {
          this.langIndex = idx;
          this.$i18n.locale = cachedLang;
        }
      }
    },
    computed: {},
    methods: {
      onLangChange(e) {
        this.langIndex = Number(e.detail.value);
        const lang = this.langValueList[this.langIndex];
        
        // 调用App.vue的全局语言切换方法
        this.$root.changeLanguage(lang);
        
        console.log('登录页面语言切换:', lang);
      },
      
      updateTabBarText() {
        const currentLang = uni.getStorageSync('app_lang') || 'zh';
        const messages = this.$i18n.messages;
        const texts = messages[currentLang]?.tabBar || messages.zh.tabBar;
        
        // 更新每个tabBar项的文本
        uni.setTabBarItem({
          index: 0,
          text: texts.drama
        });
        
        uni.setTabBarItem({
          index: 1,
          text: texts.theater
        });
        
        uni.setTabBarItem({
          index: 2,
          text: texts.recommend
        });
        
        uni.setTabBarItem({
          index: 3,
          text: texts.profile
        });
      },
		dyxx(){
			//#ifdef MP-WEIXIN
			// uni.requestSubscribeMessage({
			//   tmplIds: ['wAqsuRFgYS9XBJ2OmBgVYgZ-YrpOkS0dfo-WxQuv41c'],
			//   success (res) {
			// 	  /*uni.showModal({
			// 	  	title: '提醒',
			// 	  	content: JSON.stringify(res),
			// 	  	showCancel: false,
			// 	  	confirmText: "确定",
			// 	  	success: function (res) {
			// 	  		if (res.confirm) {
			// 	  			//uni.navigateBack();
			// 	  		} else if (res.cancel) {
				  			
			// 	  		}
			// 	  	}
			// 	  });*/
			//   },
			//   fail: (data) => {
			// 	  uni.showModal({
			// 	  	title: '提醒',
			// 	  	content: JSON.stringify(data),
			// 	  	showCancel: false,
			// 	  	confirmText: "确定",
			// 	  	success: function (res) {
			// 	  		if (res.confirm) {
			// 	  			//uni.navigateBack();
			// 	  		} else if (res.cancel) {
				  			
			// 	  		}
			// 	  	}
			// 	  });
			//   }
			// })
			//#endif
		},
      async bindLogin() {
		  
		let _this =this
        if(!this.mobile){
        	uni.showToast({ title: this.$t('login.mobile_required'),icon:"none" });
        	this.userfocus= true
        	return false;
        }
        if(!this.password){
        	uni.showToast({ title: this.$t('login.password_required'),icon:"none" });
        	this.paswfocus= true
        	return false;
        }
        let data = {'account':this.mobile,'password':this.password};
        // #ifdef APP-PLUS
        	var inf = plus.push.getClientInfo();
        	if(inf.clientid){data.clientid=inf.clientid}
        	if(plus.os.name == 'iOS') {data.iostoken=inf.token}
        	data.issb=plus.os.name
        // #endif
        
        if(this.openid){
        	data.openid=this.openid;
        }
        let [err,res] =await this.$httpas.get('/api/user/login',data);
		
        if (!this.$httpas.errorCheck(err,res)) return;
        
        if(res.data.code === 1){
				if(res.data.data.userinfo.avatar){
						var str = res.data.data.userinfo.avatar;
						if(str.indexOf("data:image") != -1){
							var avatar='';
						}else{
							if(str.indexOf("http") != -1){
								avatar=res.data.data.userinfo.avatar;
							}else{
								avatar=this.configs.imgUrl+res.data.data.userinfo.avatar;
							}
						}
					}else{
						var avatar='';
					}
			uni.setStorage({//缓存配置信息
				key: 'avatar',  
				data: avatar
			})
			uni.setStorage({//缓存配置信息
				key: 'userinfo',  
				data: res.data.data.userinfo
			})
			uni.setStorage({//缓存配置信息
				key: 'config',  
				data: res.data.data.config
			})
        	uni.showModal({
        		title: this.$t('login.notice'),
        		content: res.data.msg,
        		showCancel: false,
        		confirmText: this.$t('login.ok'),
        		success: function (res) {
        			if (res.confirm) {
						//_this.dyxx()
						
        				uni.navigateBack();
        			} else if (res.cancel) {
        				
        			}
        		}
        	});
        }else{
			uni.showToast({ title: res.data.msg,icon:"none" });
        }

      },
	  getUserInfo1(){
	  	var this_=this
	                 //console.log("aaaa")
	                 uni.getUserProfile({
	                     desc:'Wexin',     // 这个参数是必须的
	                     success:res=>{
	  					   console.log(res);
	                         this_.userinfowx=res.userInfo
	                         this_.wxlogin()
	                     },
	                     fail:err=>{
	                         console.log(err)
	                     }
	                 })
	  },
	  getuserinfo: function(){
		  uni.showLoading({
		  	title: '授权中...'
		  });
		var this_=this
	  	// wx登录
	  	wx.login({
	  	  success (res) {
	  	    if (res.code) {
	  	      //发起网络请求
	  		  var code = res.code
	  		  	// 获取微信用户信息
	  			wx.getUserInfo({
	  			  success: function(res) {
					this_.userinfowx=res.userInfo
					this_.wxlogin()
	  			  },
	  			  fail:res=>{
	  			      // 获取失败的去引导用户授权 
	  			   }
	  			})
	  			
	  	    } else {
	  			
	  	    }
	  	  }
	  	})
	  },
	  wxlogin(){
		  var userinfowx=this.userinfowx
		  var this_=this
		  uni.login({
		       timeout: 10000,
		        provider: 'weixin',  //如果是uniapp，在这里需要标明平台的类型，支持的参数请查阅uniapp官网的uni.login()文档
		        success: (res) => {
					//console.log(res);
		  			//登陆成功的回调
					uni.hideLoading();
					uni.request({
					      url: this.configs.webUrl+'/api/user/getOpenid',  
					      method:'GET',  
					      data: {   
					          //token: uni.getStorageSync("userinfo").token,       //你的小程序的secret,  
					          code: res.code                        //wx.login 登录成功后的code  
					      },  
					      success: (cts) => {  
							  if(cts.data.code==1){
								  this_.openid=cts.data.data.openid
								  this_.unionid=cts.data.data.unionid
								  uni.showModal({
								  	title: this.$t('login.notice'),
								  	content: this.$t('login.auth_first'),
								  	showCancel: false,
								  	confirmText: this.$t('login.ok'),
								  	success: function (res) {
										if (res.confirm) {
											console.log(cts.data.msg);
											if(cts.data.msg=='登录成功'){
												this_.logins(cts.data.data)
											}else{
												this_.opsdd(cts.data.data)
											}
										} else if (res.cancel) {
											
										}
										
								  	}
								  });
							  }else{
								  uni.showModal({
								  	title: this.$t('login.notice'),
								  	content: JSON.stringify(cts.data),
								  	showCancel: false,
								  	confirmText: this.$t('login.ok'),
								  	success: function (res) {
								  		if (res.confirm) {
								  			//uni.navigateBack();
								  		} else if (res.cancel) {
								  			
								  		}
								  	}
								  });
							  }
					      },
						  fail: (err1) => {
						  	console.log(err1);
						     	uni.hideLoading();
						  }
					});
		  	  },
		        fail: (err) => {
		           	uni.hideLoading();
		        }
		  })
	  },
	  logins(data){
		  if(data.userinfo.avatar){
		  			var str = data.userinfo.avatar;
		  			if(str.indexOf("data:image") != -1){
		  				var avatar='';
		  			}else{
		  				if(str.indexOf("http") != -1){
		  					avatar=data.userinfo.avatar;
		  				}else{
		  					avatar=this.configs.imgUrl+data.userinfo.avatar;
		  				}
		  			}
		  		}else{
		  			var avatar='';
		  		}
		  uni.setStorage({//缓存配置信息
		  	key: 'avatar',  
		  	data: avatar
		  })
		  uni.setStorage({//缓存配置信息
		  	key: 'userinfo',  
		  	data: data.userinfo
		  })
		  uni.setStorage({//缓存配置信息
		  	key: 'config',  
		  	data: data.config
		  })
		  if(this.fxpid>0){
		  	uni.switchTab({
		  		url:'/pages/client/member/index'
		  	})
		  }else{
				uni.navigateBack();
		  }
		  
	  },
	  opsdd(data){
		  this.userinfowx.openid=this.openid
		  uni.setStorage({//缓存配置信息
		  	key: 'userinfowx',  
		  	data: this.userinfowx
		  })
		  var urlsa1='/pages/login/loginwxxcx?fxpid='+this.fxpid
		  if(data.dltel){
			  urlsa1='/pages/login/loginwxxcx?dltel='+data.dltel+'&fxpid='+this.fxpid
		  }
		  uni.navigateTo({
		  	url:urlsa1
		  })	
	  },
	  onGetPhoneNumber(e) {
		  var this_=this
		  if(!this.openid){
			  uni.showModal({
			  		title: this.$t('login.notice'),
			  		content: this.$t('login.auth_first'),
			  		showCancel: false,
			  		confirmText: this.$t('login.ok'),
			  		success: function (res) {
			  			//this_.getuserinfo()
			  		}
			  });
			  return false;
		  }
		  uni.login({
		       timeout: 10000,
		        provider: 'weixin',  //如果是uniapp，在这里需要标明平台的类型，支持的参数请查阅uniapp官网的uni.login()文档
		        success: (res) => {
				console.log(res);
				console.log("onGetPhoneNumber", e);  
				uni.request({
					  url: this.configs.webUrl+'/api/user/getphone',  
					  method:'GET',  
					  data: {    
						  openid:this.openid,
						  userinfowx:this.userinfowx,
						  code: res.code,
						  iv: e.detail.iv,
						  encryptedData:e.detail.encryptedData,  
					  },  
					  success: (cts) => {
						  console.log(cts.data);
						  uni.showModal({
						  		title: this.$t('login.notice'),
						  		content: JSON.stringify(cts.data),
						  		showCancel: false,
						  		confirmText: this.$t('login.ok'),
						  		success: function (res) {
						  			if (res.confirm) {
						  				//uni.navigateBack();
						  			} else if (res.cancel) {
						  										
						  			}
								}
						  });
					  },
					  fail: (err1) => {
						console.log(err1);
							//失败的回调
					  }
				});
			},
			fail: (err) => {
			     	//登陆失败的回调
			},
		})
	  } 
    }
  }
</script>

<style>
.uni-page-head{
	background-color: transparent !important;
}
  .content {
    display: flex;
    flex-direction: column;
    justify-content: center;
	background: linear-gradient(to bottom, #402862, #1C062C);
    min-height: 100vh;
  }

  .header {
    width: 161rpx;
    height: 161rpx;
    background: #000;
    box-shadow: 0rpx 12rpx 13rpx 0rpx rgba(0,0,0,0.7);
    border-radius: 50%;
    margin-top: 30rpx;
    margin-left: auto;
    margin-right: auto;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .header image {
    width: 161rpx;
    height: 161rpx;
    border-radius: 50%;
    background: #222;
  }

  .header, .header * {
    color: #fff !important;
  }

  .list {
    display: flex;
    flex-direction: column;
    padding-top: 150rpx;
    padding-left: 70rpx;
    padding-right: 70rpx;
	
  }

  .list-call {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
	margin-bottom: 24rpx;
    height: 100rpx;
    color: #fff;
    border-bottom: 0.5px solid #222;
    background: #5A3F7C;
	padding: 48rpx 20rpx;
	border-radius: 16rpx;
  }

  .list-call .img {
    width: 40rpx;
    height: 40rpx;
    filter: brightness(0.8);
  }

  .list-call .sl-input {
    flex: 1;
    text-align: left;
    font-size: 32rpx;
    margin-left: 16rpx;
    background: transparent;
    color: #fff;
    border: none;
    outline: none;
    caret-color: #0f0;
  }

  .button-login {
    color: #fff;
    font-size: 34rpx;
    width: 470rpx;
    height: 100rpx;
    background: linear-gradient(to right, #A515EE, #F35CBB);
    box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(0,0,0,0.5);
    border-radius: 50rpx;
    line-height: 100rpx;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    margin-top: 100rpx;
    border: 1px solid #333;
    transition: background 0.2s;
  }
.button-login1 {
    color: #fff;
    font-size: 34rpx;
    width: 470rpx;
    height: 80rpx;
    background: linear-gradient(-90deg, #222, #111);
    box-shadow: 0rpx 0rpx 13rpx 0rpx rgba(0,0,0,0.5);
    border-radius: 50rpx;
    line-height: 80rpx;
    text-align: center;
    margin-left: auto;
    margin-right: auto;
    margin-top: 30rpx;
    border: 1px solid #333;
  }
  .button-hover {
    background: linear-gradient(-90deg, #333, #111);
  }

  .agreenment {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    font-size: 30rpx;
    margin-top: 80rpx;
    color: #fff;
    text-align: center;
    height: 40rpx;
    line-height: 40rpx;
  }

  .agreenment text {
    font-size: 24rpx;
    margin-left: 15rpx;
    margin-right: 15rpx;
    color: #0f0;
  }

  /* 额外：全局字体颜色和输入框placeholder适配深色 */
  view, text, input {
    color: #fff;
  }
  input::placeholder {
    color: #888;
    opacity: 1;
  }

  .lang-picker-abs {
    position: fixed;
    top: 30rpx;
    right: 30rpx;
    z-index: 1000;
  }
  .lang-picker {
    /* 保持原有样式 */
  }
  .lang-btn {
    background: #5A3F7C;
    color: #fff;
    border-radius: 20rpx;
    padding: 10rpx 30rpx;
    font-size: 26rpx;
    border: 1px solid #333;
    text-align: center;
    min-width: 100rpx;
    box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.3);
  }
  /* 自定义导航栏黑色风格 */
  .custom-navbar {
    width: 100vw;
    height: 88rpx;
    background: #000;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36rpx;
    font-weight: bold;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
  }
</style>
